﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MiscUtil.IO;

namespace TlvLib
{
    public class TlvPacker
    {
        public byte[] Pack(RawPacket packet)
        {
            var stream = new MemoryStream(128);
            var writer = new EndianBinaryWriter(Config.EndianBitConverter, stream);

            var flag = (short)((packet.IsRequest ? 0 : 1) << 15);


            writer.Write(flag);
            writer.Write(packet.MessageId);
            writer.Write(packet.Payload == null ? 0 : packet.Payload.Length);
            if (packet.Payload != null)
            {
                writer.Write(packet.Payload);
            }

            return stream.ToArray();
        }
    }
}
